//
//  KK3DTouchFuction.m
//  毛玻璃
//
//  Created by TOPTEAM on 2017/5/4.
//  Copyright © 2017年 TOPTEAM. All rights reserved.
//
#import "UIView+ParentController.h"
#import "KK3DTouchFuction.h"
#import "ViewController.h"
@interface KK3DTouchFuction ()

@property (nonatomic,strong)UINavigationController * shortCutItemNav;
@property (nonatomic,strong)UIApplicationShortcutItem * shortCutItem;

@end


@implementation KK3DTouchFuction

+(void)creat3DTouchFunction
{
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 9.0 ) {
        
        return;
    }
    
    UIApplicationShortcutIcon *P1 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"P1"];
    UIApplicationShortcutItem *Item_P1 = [[UIApplicationShortcutItem alloc] initWithType:@"P1" localizedTitle:@"P1" localizedSubtitle:nil icon:P1 userInfo:nil];
    
    
    
    UIApplicationShortcutIcon *P2 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"P2"];
    UIApplicationShortcutItem *Item_P2 = [[UIApplicationShortcutItem alloc] initWithType:@"P2" localizedTitle:@"P2" localizedSubtitle:nil icon:P2 userInfo:nil];
    
    
    
    UIApplicationShortcutIcon *P3 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"P3"];
    UIApplicationShortcutItem *Item_P3 = [[UIApplicationShortcutItem alloc] initWithType:@"P3" localizedTitle:@"P3" localizedSubtitle:nil icon:P3 userInfo:nil];
    
    UIApplicationShortcutIcon *P4 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"P4"];
    UIApplicationShortcutItem *item_P4 = [[UIApplicationShortcutItem alloc] initWithType:@"P4" localizedTitle:@"P4" localizedSubtitle:nil icon:P4 userInfo:nil];
    
    
    [UIApplication sharedApplication].shortcutItems = @[Item_P1, Item_P2,Item_P3, item_P4];
    
    
    
}


-(void)acessOf3DTouchWithApplication:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem window:(UIWindow *)window
{
    if (shortcutItem) {  //从快捷键进入系统，
        
//        if (window.rootViewController.childViewControllers.count < 3) {
//            
//            return;
//        }
        
        [self touchPushAction:shortcutItem window:window]; //处理跳转事件；
    }
}

- (void)touchPushAction:(UIApplicationShortcutItem *)shortcutItem window:(UIWindow *)window
{
    
//    self.shortCutItemNav = [self getCurrentViewControllerWithWindow:window];
    self.shortCutItem = shortcutItem;
    //界面加载完成时，进行跳转；
    [self performSelector:@selector(pushVC) withObject:nil afterDelay:0.3f];
}

//获取当前控制器

- (UINavigationController *) getCurrentViewControllerWithWindow:(UIWindow *)window
{
    UINavigationController *navigationController;
    
    UITabBarController *tabBarController = (UITabBarController *)window.rootViewController;
    
    if (tabBarController.selectedIndex == 0) {
        
        navigationController = window.rootViewController.childViewControllers.firstObject;
        
    }else if (tabBarController.selectedIndex == 1) {
        
        navigationController = window.rootViewController.childViewControllers[1];
        
    }else if (tabBarController.selectedIndex == 2) {
        
        navigationController = window.rootViewController.childViewControllers[2];
        
    }else if (tabBarController.selectedIndex == 3) {
        
        navigationController = window.rootViewController.childViewControllers[3];
        
    }else {
        
        navigationController = window.rootViewController.childViewControllers.firstObject;
    }
    
    return navigationController;
    
}

- (void)pushVC {

    if ([_shortCutItem.type isEqualToString:@"P1"]) {
        [KK3DTouchFuction KK_saveStr:@"P1" withID:@"PP_IMG"];


        NSLog(@"P1");
        
    }
    
    if ([_shortCutItem.type isEqualToString:@"P2"]) {
        NSLog(@"P2");
        [KK3DTouchFuction KK_saveStr:@"P2" withID:@"PP_IMG"];
    }
    
    if ([_shortCutItem.type isEqualToString:@"P3"]) {
        NSLog(@"P3");
        [KK3DTouchFuction KK_saveStr:@"P3" withID:@"PP_IMG"];
    }
    
    if ([_shortCutItem.type isEqualToString:@"P4"]) {
        NSLog(@"P4");
        [KK3DTouchFuction KK_saveStr:@"P4" withID:@"PP_IMG"];
    }
    
    if ([[UIView BKcurrentViewController] isKindOfClass:[ViewController class]]) {
        ViewController * VC=(ViewController*)[UIView BKcurrentViewController];
        [VC Change_img];
    }
    
}
+(void)KK_saveStr:(NSString *)str withID:(NSString *)IDStr{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:str forKey:IDStr];
}

+(NSString *)KK_getStr_withID:(NSString *)IDStr{
    NSUserDefaults *userDsfaults = [NSUserDefaults standardUserDefaults];
    return [userDsfaults objectForKey:IDStr];
}



@end
